package queue;



import java.util.Stack;

/*
* 用两个栈实现一个队列
* 实现思路：栈s1 s2 入队列 即push s1
* s2push s1的pop  就实现了 两个栈模拟队列
* */
public class TwoStackForQueue {
    Stack s1 = new Stack<Integer>();
    Stack s2 = new Stack<Integer>();
    //入队列
    public void push(int num){
        s1.push(num);
    }

    //出队列
    public int pop(){
        int temp= -1;
        //将s1中除栈底元素全部 pop到s2中
        if (!s1.empty()){
            while (s1.size()>1){
                s2.push(s1.pop());
            }
        }
        //弹出s1栈底元素
        if (s1.peek()==null){

        }else {
            temp = (int) s1.pop();
        }
        //将s2中的元素pop到s1中
        if (!s2.empty()){
            while(s2.size()>0){
                s1.push(s2.pop());
            }
        }

        return temp;

    }
    public static void main(String[] args) {
        TwoStackForQueue queue = new TwoStackForQueue();
        queue.s1.push(1);
        queue.s1.push(2);
        queue.s1.push(3);

        int pop = queue.pop();
        System.out.println(pop);
        System.out.println(queue.pop());
    }
}

